package thread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ArrayLsit,LikList,HashSet都不是并发安全的
 */
public class SyncDemo5 {
    public static void main(String[] args) {
        List<Integer> list =new ArrayList<>();

        List<Integer> list1= Collections.synchronizedList(list);
        Thread t1=new Thread(){
            public void run(){
                for (int i=0;i<1000;i++){
                    list.add(i);
                }
            }
        };
        Thread t2=new Thread(){
            public void run(){
                for (int i = 0; i < 1000; i++) {
                    list.add(i);
                }
            }
        };
        t1.start();
        t2.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}
